#include "ASFTSettingScreen.h"
#include "ASScreenMgr.h"
#include "ASScreenShare.h"
#include "..\app\ASAppConst.h"
#include "..\app\ASApp.h"
#include "ASConfirmScreen.h"
#include "..\app\ASFTData.h"
#include "..\app\ASFTIo.h"
#include "..\control\ASButton.h"
#include "ASCustomScreen.h"

static as_ft_setting_screen* s_as_ft_setting_scr_p;

static void as_ft_setting_reset_default(void)
{
    as_ft_reset_default();
    as_screen_go_back();
}

static void as_ft_show_reset_default_confirm(void)
{
    as_show_confirm_screen(TEXT_RESET_DEFAULT_CONFIRM, TEXT_OK, TEXT_CANCEL,
                           as_ft_setting_reset_default, as_screen_go_back);
}

static void as_ft_setting_screen_add_listitem(as_list* list_p, as_rect pos, ASSTR text, as_void_func func)
{
    as_listitem* li_p;
    as_label* lb_p;
    ASUINT param[4];
    li_p = list_p->add_item((as_control*)list_p, pos);

    if(li_p)
    {
        li_p->set_bg_color((as_control*)li_p, DATA_SHEET_BG_COLOR);
        li_p->set_bg_selected_image((as_control*)li_p, as_ufg_tmp(0, IMG_NML_LIST_HL), AS_IMAGE_FROM_RES);

        if(func)
            li_p->set_pen_up_cb((as_control*)li_p, (as_listitem_cb)func);

        param[0] = (ASUINT)as_ucs2_from_gb2312(text);
        lb_p = (as_label*)li_p->add_ctrl((as_control*)li_p, AS_CTRL_LABEL, pos, 1, (void**)param);

        if(lb_p)
        {
            lb_p->set_text_color((as_control*)lb_p, EDIT_TEXT_COLOR);
            lb_p->set_bg_hidden((as_control*)lb_p, ASTRUE);
            lb_p->set_layout((as_control*)lb_p, AS_LABEL_TEXT_LEFT | AS_LABEL_TEXT_V_CENTER);
        }
    }
}

static void as_ft_setting_screen_gen_list(as_list* list_p)
{
    as_rect pos;
    as_memcpy(&pos, &list_p->base.area, sizeof(as_rect));
    pos.h = NORMAL_LIST_ITEM_H;
    //as_ft_setting_screen_add_listitem(list_p,pos,TEXT_HOMEPAGE_SETTING,ASNULL);
    //pos.y += pos.h;
    //as_ft_setting_screen_add_listitem(list_p,pos,TEXT_INCOME_TYPE_MGR,ASNULL);
    //pos.y += pos.h;
    //as_ft_setting_screen_add_listitem(list_p,pos,TEXT_PAY_TYPE_MGR,ASNULL);
    //pos.y += pos.h;
    //as_ft_setting_screen_add_listitem(list_p,pos,TEXT_MEMBER_TYPE_MGR,ASNULL);
    //pos.y += pos.h;
    as_ft_setting_screen_add_listitem(list_p, pos, TEXT_RESET_DEFAULT, as_ft_show_reset_default_confirm);
    list_p->set_selected_idx((as_control*)list_p, 0);
}

static void as_ft_setting_screen_create_list(as_screen* screen_p, as_rect area)
{
    as_list* list_p = (as_list*)screen_p->add_ctrl(screen_p, AS_CTRL_LIST, area, 0, ASNULL);

    if(!list_p)
        return;

    list_p->set_bg_color((as_control*)list_p, DATA_SHEET_BG_COLOR);
    s_as_ft_setting_scr_p->list_p = list_p;
    as_ft_setting_screen_gen_list(s_as_ft_setting_scr_p->list_p);
}

static void as_ft_setting_screen_lsk(void)
{
    if(s_as_ft_setting_scr_p && s_as_ft_setting_scr_p->list_p)
    {
        if(s_as_ft_setting_scr_p->list_p->select_item && s_as_ft_setting_scr_p->list_p->select_item->pen_up_cb)
        {
            s_as_ft_setting_scr_p->list_p->select_item->pen_up_cb(s_as_ft_setting_scr_p->list_p->select_item);
        }
    }
}

static void as_ft_setting_screen_create_bottom_bar(as_screen* screen_p, as_rect area)
{
    as_button* btn_p;
    as_screen_create_bar_img(screen_p, area, IMG_BOTTOM);
    area.w = TEXT_SK_W;
    btn_p = as_screen_create_lsk(screen_p, area, TEXT_SELECT, as_ft_setting_screen_lsk);

    if(btn_p)
        btn_p->add_keycode((as_control*)btn_p, AS_KEY_OK);

    area.w = TEXT_SK_W;
    area.x = SCR_W - area.w;
    as_screen_create_rsk(screen_p, area, TEXT_BACK, as_show_custom_screen_from_child);
}

void as_ft_setting_screen_init(as_screen* screen_p)
{
    as_rect area;

    if(!screen_p)
        return;

    screen_p->set_bg_color(screen_p, DATA_SHEET_BG_COLOR);
    s_as_ft_setting_scr_p = (as_ft_setting_screen*)screen_p;
    area.x = 0;
    area.y = as_app_get_advbar_height();
    area.w = SCR_W;
    area.h = SMALL_TOP_BAR_H;
    as_screen_create_title_bar(screen_p, area, IMG_SMALL_TOP, IMG_TITLE_SETTING);
    area.x += 2;
    area.w = SCR_W - area.x * 2;
    area.y += area.h;
    area.h = SCR_H - BOTTOM_BAR_HEIGHT - area.y;
    as_ft_setting_screen_create_list(screen_p, area);
    area.w = SCR_W;
    area.h = BOTTOM_BAR_HEIGHT;
    area.x = 0;
    area.y = SCR_H - area.h;
    as_ft_setting_screen_create_bottom_bar(screen_p, area);

    if(as_app_get_show_advbar())
    {
        area.x = area.y = 0;
        area.w = SCR_W;
        as_screen_add_advbar(screen_p, area);
    }
}
void as_ft_setting_screen_deinit(as_screen* screen_p)
{
    AS_UNUSED(screen_p);
}
void as_enter_ft_setting_screen(void)
{
    as_screen_manager* scr_mgr_p = as_get_screen_stack_instance();
    as_screen* scr_p;
    scr_mgr_p->new_and_push_screen(AS_SCREEN_FTSETTING);
    scr_p = scr_mgr_p->get_cur_screen();

    if(scr_p)
    {
        scr_p->paint_hdlr(scr_p);
        scr_p->flush_hdlr(scr_p);
    }

    as_custom_screen_set_if_goback_reload(ASFALSE);
}